package com.ruoyi.house_sale.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.house_sale.domain.ExamMaster;
import com.ruoyi.house_sale.domain.Examresult;
import com.ruoyi.house_sale.domain.ProjectInfo;
import com.ruoyi.house_sale.service.IEarnestmoneyService;
import com.ruoyi.house_sale.service.IExamMasterService;
import com.ruoyi.house_sale.service.IExamresultService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.house_sale.domain.ResearchExam;
import com.ruoyi.house_sale.service.IResearchExamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 调研答卷名称Controller
 * 
 * @author ruoyi
 * @date 2022-03-28
 */
@RestController
@RequestMapping("/house_sale/ResearchExam")
public class ResearchExamController extends BaseController
{
    @Autowired
    private IResearchExamService researchExamService;
    @Autowired
    private IExamMasterService examMasterService;
    @Resource
    private IExamresultService examresultService;
    @GetMapping("/exammaster/{reseachid}")
    public AjaxResult resultlist(@PathVariable("reseachid") Long reseachid)
    {
        List<ExamMaster> list = examMasterService.selectExamMasterList(reseachid);
        ResearchExam researchExam = researchExamService.selectResearchExamByReseachid(reseachid);

        AjaxResult ajax = AjaxResult.success();
        ajax.put("examMastertList", list);
        ajax.put("researchExam", researchExam);
        return ajax;
    }
    @PutMapping("/examresult")
    public AjaxResult listExamResult(@RequestBody Examresult examresult)
    {
        Long examid = examresult.getExamid();
        Long custid = examresult.getCustid();
        Examresult examResult = examresultService.selectByExamId(examid,custid);
        AjaxResult ajax = AjaxResult.success();
        if(examResult!=null){
            Long resultid = examResult.getResultid();
            ajax.put("resultid", resultid);
        }
        ajax.put("examresult", examResult);

        return ajax;
    }
    @PutMapping("/examResultList")
    public AjaxResult ExamResultList(@RequestBody Examresult examresult)
    {
        Long reseachid = examresult.getReseachid();
        Long custid = examresult.getCustid();
        List<Examresult> list  = examresultService.selectByReseachId(reseachid,custid);

        AjaxResult ajax = AjaxResult.success();
        ajax.put("examResultList", list);

        return ajax;
    }
    @PutMapping("/examMasterResult")
    public AjaxResult updResult(@RequestBody ExamMaster examMaster)
    {
        return toAjax(examMasterService.updateResult(examMaster));

    }
    /**
     * 查询调研答卷名称列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ResearchExam:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResearchExam researchExam)
    {
        startPage();
        List<ResearchExam> list = researchExamService.selectResearchExamList(researchExam);

        return getDataTable(list);
    }

    @GetMapping("/projectList")
    public AjaxResult projectList(){

        List<ProjectInfo> projectList = researchExamService.findAllProject();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("projectList", projectList);
        return ajax;
    }

    /**
     * 导出调研答卷名称列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ResearchExam:export')")
    @Log(title = "调研答卷名称", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchExam researchExam)
    {
        List<ResearchExam> list = researchExamService.selectResearchExamList(researchExam);
        ExcelUtil<ResearchExam> util = new ExcelUtil<ResearchExam>(ResearchExam.class);
        util.exportExcel(response, list, "调研答卷名称数据");
    }

    /**
     * 获取调研答卷名称详细信息
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ResearchExam:query')")
    @GetMapping(value = "/{reseachid}")
    public AjaxResult getInfo(@PathVariable("reseachid") Long reseachid)
    {
        List<ProjectInfo> projectList = researchExamService.findAllProject();
        ResearchExam researchExam = researchExamService.selectResearchExamByReseachid(reseachid);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("projectList", projectList);
        ajax.put("researchExam", researchExam);
        return ajax;
    }

    /**
     * 新增调研答卷名称
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ResearchExam:add')")
    @Log(title = "调研答卷名称", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ResearchExam researchExam)
    {
        return toAjax(researchExamService.insertResearchExam(researchExam));
    }

    /**
     * 修改调研答卷名称
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ResearchExam:edit')")
    @Log(title = "调研答卷名称", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchExam researchExam)
    {
        return toAjax(researchExamService.updateResearchExam(researchExam));
    }

    /**
     * 删除调研答卷名称
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ResearchExam:remove')")
    @Log(title = "调研答卷名称", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reseachids}")
    public AjaxResult remove(@PathVariable Long[] reseachids)
    {
        return toAjax(researchExamService.deleteResearchExamByReseachids(reseachids));
    }

    //答卷的状态改变
    @PreAuthorize("@ss.hasPermi('house_sale:ResearchExam:edit')")
    @Log(title = "调研答卷名称", businessType = BusinessType.UPDATE)
    @PutMapping("/changeinvalid")
    public AjaxResult changeinvalid(@RequestBody ResearchExam researchExam)
    {
        researchExam.setUpdateBy(getUsername());
        return toAjax(researchExamService.changeinvalid(researchExam));
    }
}
